package assdraw2.historyagent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import assdraw2.canvas.abstraction.Shape;
import assdraw2.common.MessageType;
import assdraw2.common.PACAgent;
import assdraw2.common.PACMessage;
import assdraw2.historyagent.abstraction.HistoryAbstraction;
import assdraw2.historyagent.presentation.HistoryPresentation;

public class HistoryControl implements PACAgent{

	private PACAgent parent;
	
	private HistoryAbstraction abstraction = new HistoryAbstraction();
	private HistoryPresentation presentation;
	
	public HistoryControl(PACAgent parent) {
		super();
		this.parent = parent;
	}

	@Override
	public void receiveMessage(PACMessage m) {
		Date dateNow = new Date ();
		 
        SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("dd.MM.yyyy / h:m:s");
 
        StringBuilder time = new StringBuilder( dateformatYYYYMMDD.format( dateNow ) );
        
		if(m.getType() == MessageType.OPEN){		
			presentation = new HistoryPresentation(this);
			return;
		}else if(m.getType() == MessageType.CLOSE){
			presentation.dispose();
			return;
		}
		
		
		else if(m.getType() == MessageType.SHAPE_ADDED){
			abstraction.add(time + " : " + "Shape " + m.getContent().toString() + " added at " + ((Shape)m.getContent()).getX() +","+((Shape)m.getContent()).getY());
		}else if(m.getType() == MessageType.SHAPE_DELETED){
			abstraction.add(time + " : " + "Shape(s) deleted.");
		}else if(m.getType() == MessageType.SHAPE_MOVED){
			abstraction.add(time + " : " + "Shape "+ m.getContent().toString()+ " Z order changed.");
		}else if(m.getType() == MessageType.SHAPE_SELECTION){
			abstraction.add(time + " : " + "Shape "+ m.getContent().toString()+" added to selection.");
		}else if(m.getType() == MessageType.SHAPE_UNGROUP){				
			abstraction.add(time + " : " + "Selected shapes ungrouped.");
		}else if(m.getType() == MessageType.SHAPE_GROUP){				
			abstraction.add(time + " : " + "Selected shapes grouped.");
		}
		
		if(presentation != null){
			presentation.getList().setListData(abstraction);
		}
	}

	@Override
	public void sendMessage(PACMessage m) {
		if(m.getType() == MessageType.CLOSE){
			parent.receiveMessage(m);
		}
	}

	public Vector<String> getData() {
		return abstraction;
	}

	public PACAgent getParentAgent() {
		return parent;
	}
}
